package com.aaron.test;

import org.apache.commons.lang.StringUtils;

/**
 * @author liurong
 * @date 2021/8/11 18:13
 * @version:
 */
public class T111 {

    public static String changeAddressV2(String address) {
        if (StringUtils.isBlank(address)) {
            return "";
        }
//        addrFlagArray
        //地理位置标志
        String addrFlag = "小区 省 州 县 市 乡 镇 村 居 街 路 号 弄 层 室 楼 区 ";
        String specialAddrFlag = "公馆 苑 庭 湾 园";
        String[] addrArraySplitByFlag = address.split("["+addrFlag+specialAddrFlag+"]");
        String[] addrFlagArray = (addrFlag+specialAddrFlag).split(" ");
        StringBuffer sb = new StringBuffer();
        for (int i = 0 ; i < addrArraySplitByFlag.length ; i++ ) {//循环分隔结果
            for (String flag : addrFlagArray) {//循环地理位置特征
                String s = addrArraySplitByFlag[i];
                int index = address.indexOf(s);
                if(index+flag.length()+s.length() <= address.length() && flag.equals(address.substring(index+s.length(),index+flag.length()+s.length()))){
                    String replace = s ;
                    replace = replace.replace("(","[(]");//?这个有问题
                    replace = replace.replace(")","[)]");
                    address = address.replaceFirst(replace+ flag,"");

                    if(!flag.equals("区")  ){
                        replace = s.replaceAll("(\\d)", "*");//数字脱敏
                        replace = replace.replaceAll("(\\D)", "*");//数字脱敏
                    }
                    if (specialAddrFlag.contains(flag)) {
                        int len = replace.length()+ flag.length();
                        for (int j = 0; j < len; j++) {
                            sb.append("*");
                        }
                        break;
                    }
//					addrArraySplitByFlag[i] = replace;
                    sb.append(replace+flag);
                    break;
                }
            }
        }
        address = sb.toString().replaceAll("(\\d)", "*")+address;//数字脱敏
        return address;
    }

    public static void main(String[] args) {
        System.out.println(changeAddressV2("上海市奉贤区奉城镇绿色家园asdjflkasd12321"));
        System.out.println(changeAddressV2("而得到的道德的0智能井盖02上海省奉贤区(12智3州1智23县1智23市1智23乡1智23镇12智12小区12智12弄12智12层12智2层12智12号上海省奉贤区)(12智3州1智23县1智23市1智23乡1智23镇12智12小区12智12弄12智12层12智2层12智12号而得到的道德的)"));
    }
}

